package com.siwei.mes.sync.erp.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.siwei.mes.common.validator.bus.SyncGroup;
import com.siwei.mes.entity.sync.tc.Waybill;
import com.siwei.mes.enums.SyncEnum;
import com.siwei.mes.sync.erp.enums.ErpOrgState;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * ERP运单入参
 *
 * @Description: ERP运单入参
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/8/21 22:00
 */
@Data
public class SyncErpWaybill {
    @JSONField(name = "WLID")
    @JsonProperty(value = "WLID")
    @ApiModelProperty(value = "磅单id")
    @NotNull(groups = {SyncGroup.class}, message = "磅单id为空")
    private Long WLID;
    @JSONField(name = "code")
    @JsonProperty(value = "code")
    @ApiModelProperty(value = "磅单编号")
    @NotBlank(groups = {SyncGroup.class}, message = "磅单编号为空")
    private String code;
    @JSONField(name = "driverName")
    @JsonProperty(value = "driverName")
    @ApiModelProperty(value = "司机姓名")
    private String driverName;
    @JSONField(name = "driverTel")
    @JsonProperty(value = "driverTel")
    @ApiModelProperty(value = "司机电话")
    private String driverTel;
    @JSONField(name = "carLicenseNum")
    @JsonProperty(value = "carLicenseNum")
    @ApiModelProperty(value = "车牌")
    private String carLicenseNum;
    @JSONField(name = "yclName")
    @JsonProperty(value = "yclName")
    @ApiModelProperty(value = "材料名称")
    @NotBlank(groups = {SyncGroup.class}, message = "材料名称为空")
    private String yclName;
    @JSONField(name = "pzgg")
    @JsonProperty(value = "pzgg")
    @ApiModelProperty(value = "材料规格")
    @NotBlank(groups = {SyncGroup.class}, message = "材料规则为空")
    private String pzgg;
    @JSONField(name = "category")
    @JsonProperty(value = "category")
    @ApiModelProperty(value = "粗骨料，细骨料，水泥，矿粉，水，掺合料，外加剂 其它")
    @NotBlank(groups = {SyncGroup.class}, message = "材料类型为空")
    private String category;
    @JSONField(name = "newNet")
    @JsonProperty(value = "newNet")
    @ApiModelProperty(value = "签收数量(kg)")
    @NotNull(groups = {SyncGroup.class}, message = "签收数量为空")
    private BigDecimal newNet;
    @JSONField(name = "totalWeight")
    @JsonProperty(value = "totalWeight")
    @ApiModelProperty(value = "毛重(kg)")
    private BigDecimal totalWeight;
    @JSONField(name = "tare")
    @JsonProperty(value = "tare")
    @ApiModelProperty(value = "皮重(kg)")
    private BigDecimal tare;
    @JSONField(name = "netWeight")
    @JsonProperty(value = "netWeight")
    @ApiModelProperty(value = "净重(kg)")
    private BigDecimal netWeight;
    @JSONField(name = "factory")
    @JsonProperty(value = "factory")
    @ApiModelProperty(value = "生产厂家")
    @NotBlank(groups = {SyncGroup.class}, message = "生产厂家为空")
    private String factory;
    @JSONField(name = "orgState")
    @JsonProperty(value = "orgState")
    @ApiModelProperty(value = "未装货，装货中，已装货，未卸货，卸货中，已卸货，已作废，已确认")
    private String orgState;
    @JSONField(name = "initialTotalWeight")
    @JsonProperty(value = "initialTotalWeight")
    @ApiModelProperty(value = "出厂毛重")
    private BigDecimal initialTotalWeight;
    @JSONField(name = "initialTare")
    @JsonProperty(value = "initialTare")
    @ApiModelProperty(value = "出厂皮重")
    private BigDecimal initialTare;
    @JSONField(name = "initialNetWeight")
    @JsonProperty(value = "initialNetWeight")
    @ApiModelProperty(value = "出厂净重")
    private BigDecimal initialNetWeight;

    @JSONField(name = "supplyCompanyCompany")
    @JsonProperty(value = "supplyCompanyCompany")
    @ApiModelProperty(value = "供货商名称")
    //@NotBlank(groups = {SyncGroup.class}, message = "供货商名称为空")
    private String supplyCompanyCompany;

    /**
     * 组装品控waybill实体
     *
     * @param syncInfo
     * @return
     */
    public static Waybill payloadInfo(SyncErpWaybill syncInfo) {
        Waybill entity = new Waybill();
        entity.setWaybillId(syncInfo.getWLID());
        entity.setWaybillCode(syncInfo.getCode());
        entity.setDriverName(syncInfo.getDriverName());
        entity.setDriverTel(syncInfo.getDriverTel());
        entity.setVehicleNum(null);
        entity.setVehiclePlate(syncInfo.getCarLicenseNum());
        entity.setProjectName(syncInfo.getYclName());
        entity.setProjectSpecs(syncInfo.getPzgg());
        entity.setFactory(syncInfo.getFactory());
        entity.setSupplyCompanyCompany(syncInfo.getSupplyCompanyCompany());

        entity.setTransportQuantity(BigDecimal.ZERO);
        entity.setSignQuantity(BigDecimal.ZERO);
        entity.setNetQuantity(BigDecimal.ZERO);

        if (syncInfo.getOrgState().equals(ErpOrgState.WXH) && syncInfo.getInitialNetWeight() != null) {
            entity.setTransportQuantity(syncInfo.getInitialNetWeight().divide(new BigDecimal(1000), 3, RoundingMode.HALF_UP));
            entity.setSignQuantity(syncInfo.getInitialNetWeight().divide(new BigDecimal(1000), 3, RoundingMode.HALF_UP));
            entity.setNetQuantity(syncInfo.getInitialNetWeight().divide(new BigDecimal(1000), 3, RoundingMode.HALF_UP));
        }

        if (syncInfo.getOrgState().equals(ErpOrgState.YXH) && syncInfo.getNetWeight() != null) {
            entity.setTransportQuantity(syncInfo.getNetWeight().divide(new BigDecimal(1000), 3, RoundingMode.HALF_UP));
            entity.setSignQuantity(syncInfo.getNetWeight().divide(new BigDecimal(1000), 3, RoundingMode.HALF_UP));
            entity.setNetQuantity(syncInfo.getNetWeight().divide(new BigDecimal(1000), 3, RoundingMode.HALF_UP));
        }
        entity.setAbnormalQuantity(BigDecimal.ZERO);
        entity.setGrossQuantity(syncInfo.getTotalWeight());
        entity.setTareQuantity(syncInfo.getTare());
        entity.setHasAbnormal(0);
        entity.setAbnormalInfo(null);
        entity.setSyncType(SyncEnum.ERP.getCode());
        return entity;
    }
}
